﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using MySql.Data.MySqlClient;
using Bass.Tools;

public partial class userregister : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        if (Request.Cookies["CheckCode"] == null)
        {
            Response.Write("<script>alert('您的浏览器设置已被禁用 Cookies，您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。');location='userregister.aspx'</script>");
            //lblMessage.Text = "您的浏览器设置已被禁用 Cookies，您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
            //lblMessage.Visible = true;
            return;
        }

        if (String.Compare(Request.Cookies["CheckCode"].Value, txtCheckCode.Text, true) != 0)
        {
            Response.Write("<script>alert('*验证码错误');</script>");
            //lblMessage.Text = "*验证码错误";
            //lblMessage.Visible = true;
            return;
        }

        if (IsValid == true)
        {
            //OleDbConnection Conn = new OleDbConnection();
            //Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("miumiu.mdb");

            string username = TextBox1.Text.ToString();
            string userpassword = TextBox3.Text.ToString();
            string userpassword_md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(userpassword, "md5");
            string useremail = TextBox6.Text.ToString();
            string usermobile = TextBox7.Text.ToString();
            string question = TextBox11.Text.ToString();
            string answer = TextBox12.Text.ToString();

            //Conn.Open();

            //取到连接字符串
            string propertiesPath = Server.MapPath("mysqldb.properties");
            mytools mt = new mytools();
            string ConnString = mt.getMysqlConn(propertiesPath);

            //打开连接
            MySqlConnection myConnection = new MySqlConnection(ConnString);
            myConnection.Open();

            
            //string query = "select count(*) from user_info where username='" + username + "'";
            //MySqlCommand myCommand = new MySqlCommand(query, myConnection);
            //int count = Convert.ToInt32(myCommand.ExecuteScalar());

            //执行SQL
            string sqlinsert = "insert into user_info (username,userpassword,email,mobilephone,question,answer,pictureaddress)values('" + username + "','" + userpassword_md5 + "','" + useremail + "','" + usermobile + "','" + question + "','" + answer + "','')";

            MySqlCommand Comminsert = new MySqlCommand(sqlinsert, myConnection);
            MySqlTransaction myTransaction = myConnection.BeginTransaction();
            Comminsert.Transaction = myTransaction;
            try
            {
                Comminsert.ExecuteNonQuery();
                myTransaction.Commit();    //提交事务　
            }
            catch
            {
                myTransaction.Rollback();  //回滚事务
                Response.Write("<script>alert('对不起！操作失败！');location='userregister.aspx'</script>");
                myConnection.Close();
                return;
            }

            ClearTextBox();
            //string mes1 = "恭喜！您已注册成功！";

            //创建学生文件夹
            //string CurrentDirectory = Server.MapPath(".") + "/users";
            //Directory.SetCurrentDirectory(CurrentDirectory);
            //Directory.CreateDirectory(username);


            Response.Write("<script>alert('恭喜！您已注册成功！');location='index.aspx'</script>");
            myConnection.Close();
        }

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("index.aspx");
    }
    protected void ClearTextBox()
    {
        TextBox1.Text = string.Empty;
        TextBox3.Text = string.Empty;
        TextBox4.Text = string.Empty;
        txtCheckCode.Text = string.Empty;
        TextBox6.Text = string.Empty;
        TextBox7.Text = string.Empty;
        TextBox11.Text = string.Empty;
        TextBox12.Text = string.Empty;
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        //OleDbConnection Conn = new OleDbConnection();
        //Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("miumiu.mdb");
        //Conn.Open();
        //OleDbCommand cmd = new OleDbCommand("select count(*) from user_info where username='" + username + "'", Conn);
        //int count = Convert.ToInt32(cmd.ExecuteScalar());

        /*
        
        string mysqlid="";
        string mysqlpassword = ""; 
        string mysqldatabase="";


        StreamReader fileStream = new StreamReader(Server.MapPath("mysqldb.properties"));
        while (!fileStream.EndOfStream)
        {
            string ms = fileStream.ReadLine();
            //Response.Write("<script>alert('" + ms + "');</script>");
            string[] sArray = ms.Split('=');
            if(sArray[0]=="id")
            {
                mysqlid=sArray[1];
            }
            if(sArray[0]=="password")
            {
                mysqlpassword = sArray[1];
            }
            if(sArray[0]=="database")
            {
                mysqldatabase = sArray[1];
            }
        }
        fileStream.Close();

        string ConnString = "server=localhost;user id=" + mysqlid + ";password=" + mysqlpassword + ";database=" + mysqldatabase;
         */

        string username = this.TextBox1.Text.ToString();

        //取到连接字符串
        string propertiesPath = Server.MapPath("mysqldb.properties");
        mytools mt = new mytools();
        string ConnString = mt.getMysqlConn(propertiesPath);
        
        //打开连接
        MySqlConnection myConnection = new MySqlConnection(ConnString);
        myConnection.Open(); 

        //执行SQL
        string query = "select count(*) from user_info where username='" + username + "'";     
        MySqlCommand myCommand = new MySqlCommand(query, myConnection); 
        int count = Convert.ToInt32(myCommand.ExecuteScalar());

        myConnection.Close();

        if (count > 0)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
}
